﻿/* 07. Sorting an array means to arrange its elements in increasing order. 
 * Write a program to sort an array. Use the "selection sort" algorithm: 
 * Find the smallest element, move it at the first position,
 * find the smallest from the rest, move it at the second position, etc.*/

using System;

    class SortingArraySelectionSort
    {
        static void Main()
        {
            int smallestEl = int.MaxValue;
            int positionWithMinValue=0;
            Console.Write("Please enter the size of the array: ");
            int size = int.Parse(Console.ReadLine());
            int[] array = new int[size];         

            Console.WriteLine("Please enter the elements of the array: ");
            // reading the array
                for (int index = 0; index < array.Length; index++)
                {
                    Console.Write("Array[{0}]= ", index);
                    array[index] = int.Parse(Console.ReadLine());
                }
            //sorting the array            
            for (int m = 0; m < size; m++)
            {
                for (int i = m; i < size; i++)
                {
                    if (array[i] < smallestEl)
                    {
                        smallestEl = array[i];
                        positionWithMinValue = i;
                    }
                }
                int temp=array[m];
                array[m] = array[positionWithMinValue];
                array[positionWithMinValue] = temp;               
                smallestEl = int.MaxValue;
            }                               

            Console.Write("The sorted array is: ");
            for (int i = 0; i < size; i++)
            {
               Console.Write("{0} ", array[i]);
            }
        }
    }

